<template>
  <view class="register-container">
    <view class="logo-container">
      <image src="../../static/assets/lanqiao-logo.svg" mode="aspectFit" class="logo"></image>
    </view>
    <view class="input-container">
      <input type="number" placeholder="请输入手机号" v-model="phone" />
      <input type="password" placeholder="请输入密码" v-model="password" />
      <input type="password" placeholder="确认密码" v-model="confirmPassword" />
    </view>
    <button class="register-button" @click="register">注册</button>
    <navigator class="login-link" url="/pages/login/login">已有账号去登录</navigator>
  </view>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      phone: '',
      password: '',
      confirmPassword: ''
    };
  },
  methods: {
    generateRandomName() {
      const letters = 'abcdefghijklmnopqrstuvwxyz';
      let randomName = '';
      for (let i = 0; i < 4; i++) {
        const randomIndex = Math.floor(Math.random() * letters.length);
        randomName += letters.charAt(randomIndex);
      }
      return randomName;
    },
    generateUserId() {
      const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
      let userId = '';
      for (let i = 0; i < 4; i++) {
        const randomIndex = Math.floor(Math.random() * characters.length);
        userId += characters.charAt(randomIndex);
      }
      return userId;
    },
    async register() {
      if (!this.phone ||!this.password ||!this.confirmPassword) {
        uni.showToast({
          title: '手机号、密码和确认密码不能为空',
          icon: 'none'
        });
        return;
      }
      // 验证手机号格式
      if (!/^1\d{10}$/.test(this.phone)) {
        uni.showToast({
          title: '无效的手机号',
          icon: 'none'
        });
        return;
      }
      if (this.password!== this.confirmPassword) {
        uni.showToast({
          title: '两次输入的密码不一致',
          icon: 'none'
        });
        return;
      }

      const randomName = this.generateRandomName();
      const userId = this.generateUserId();

      // 使用axios发送注册信息到user接口
      try {
        await axios.post('http://localhost:3000/user', {
          userid: userId,
          username: this.phone,
          password: this.password,
          name: randomName
        });
        uni.showToast({
          title: '注册成功',
          icon:'success',
          
          success: () => {
            // 注册成功后跳转到登录页面
            uni.redirectTo({
              url: '/pages/login/login'
            });
          }
        });
      } catch (error) {
        console.error('注册失败:', error);
        uni.showToast({
          title: '注册失败，请稍后重试',
          icon: 'none'
        });
      }
    }
  }
};
</script>

<style scoped>
/* 整体背景样式 */
page {
  background-color: #f8f8f8;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}

.register-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  width: 400px;
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.logo-container {
  margin-bottom: 30px;
}

.logo {
  width: 120px;
  height: 120px;
  filter: drop-shadow(0 0 10px rgba(0, 0, 0, 0.1));
}

.input-container {
  width: 100%;
  margin-bottom: 20px;
}

.input-container input {
  width: 350px;
  margin: auto;
  padding: 18px;
  margin-bottom: 20px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

.input-container input:focus {
  outline: none;
  border-color: #007AFF;
  box-shadow: 0 0 10px rgba(0, 122, 255, 0.2);
}

.register-button {
  width: 386px; 
  padding: 5px;
  background-color: #007AFF;
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 18px;
  margin-bottom: 20px;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.register-button:hover {
  background-color: #0056b3;
  transform: scale(1.05);
}

.login-link {
  color: #333;
  text-decoration: none;
  transition: color 0.3s ease;
  font-size: 14px;
  text-align: center; /* 使文字居中，视觉上更美观 */
  width: 100%; /* 使登录链接宽度和其他元素一致 */
}

.login-link:hover {
  color: #007AFF;
}
</style>